package com.shuo.dsa.leetcode;

import java.util.Arrays;

/**
 * int2Str
 */
public class Itoa {
    public static String int2String(int value) {
        boolean isNegative = false;
        if (value < 0) {
            value = (-value);
            isNegative = true;
        }
        int calValue = value;
        int size = 0;
        while (calValue > 0) {
            calValue /= 10;
            size++;
        }
        if (isNegative) size++;
        int num = 1;
        char[] chars = new char[size];
        int len = 0;
        while (value > num) {
            int sNum = value % (10 * num) / num;
            chars[len++] = (char) (sNum + '0');
            num *= 10;
        }
        if (isNegative)
            chars[len++] = '-';
        reverse(chars, 0, len - 1);
        System.out.println(Arrays.toString(chars));
        return String.valueOf(chars);
    }

    private static void reverse(char[] array, int start, int end) {
        while (start < end) {
            char temp = array[start];
            array[start] = array[end];
            array[end] = temp;
            start++;
            end--;
        }
    }

    public static void main(String[] args) {
        System.out.println(int2String(-9990010));
    }
}
